{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "#### 4.6.1 Null值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//相等和不相等\n",
    "string? uri=null;\n",
    "if ( uri !=null)\n",
    "{\n",
    "   System.Console.WriteLine($\"\"Uri is:{uri});\n",
    "}else\n",
    "{\n",
    "    System.Console.WriteLine(\"Uri is null\");\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//ReferenceEquals()方法\n",
    "#nullable enable  //应为 \"enable\"、\"disable\" 或 \"restore\"   开启  关闭   恢复\n",
    "string? uri=null;\n",
    "//...\n",
    "if (object.ReferenceEquals(uri,null))\n",
    "{\n",
    "    System.Console.WriteLine(\"Uri is null\");\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "- ReferenceEquals()方法  \n",
    "    > 该方法用于判断两个引用型变量是否指向了内存中的同个对象,而不是判断两个对象相同的内容。如果两个变量指向了不同的对象,即使内容相同，则该方法一定返回false."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "// isnull 匹配操作符\n",
    "if (uri is object) //通过测试一个变量是滞是一个对象来判断其是否不为null值.\n",
    "{\n",
    "  System.Console.WriteLine($\"uri is :{uri}\");\n",
    "\n",
    "}else\n",
    "{\n",
    "    System.Console.WriteLine(Uri is null);\n",
    "}\n",
    "\n",
    "//C#7.0\n",
    "if (uri is null)\n",
    "{\n",
    "  System.Console.WriteLine($\"uri is :{uri}\");\n",
    "\n",
    "}else\n",
    "{\n",
    "    System.Console.WriteLine(Uri is null);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//is{}模式匹配操作符\n",
    "#nullable disable\n",
    "int? uri;\n",
    "if (uri is {})\n",
    "{\n",
    "    System.Console.WriteLine($\"Uri is :{uri}\");\n",
    "\n",
    "}else\n",
    "{\n",
    "    System.Console.WriteLine(\"Uri is null\");\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "#### 4.6.2 空合并操作符与空合并赋值操作符"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "> 空合并操作??\n",
    "- 如果这个值为空,就使用另一个值,其形式如下:  \n",
    "    - expression1 ?? expression2   //表达式   \n",
    "    - ??操作符支持知短路就值,如1值不为空,就返回1值,另一个值2不求值.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "// 4.36 空合并操作符\n",
    "string? fullName=GetDefaultDirectory();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "// 4.36 空合并操作符\n",
    "#nullable enable\n",
    "using System.IO;\n",
    "string? fullName =Directory.GetCurrentDirectory();\n",
    "System.Console.WriteLine(fullName);\n",
    "string fileName=System.IO.Path.GetFileName(fullName)??\"config.json\";\n",
    "string directory=Directory.GetCurrentDirectory()??System.AppDomain.CurrentDomain.BaseDirectory??System.Environment.CurrentDirectory;\n",
    "System.Console.WriteLine(directory);\n",
    "fullName ??=$\"{directory}/{fileName}\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "- 空合并操作符能完善“链接”。从左向右选出第一个非null表达式。\n",
    "- C#8.0引入了空合并赋值操作符，可以简单地理解为j:如果等于号左侧的变量不为null，则维持其原值不变，否则将用等于号右侧表达式的会下对等于号左侧的变量进行赋值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "#### 4.6.3 空条件操作符\n",
    "C#6.0 引入了?. 操作符，称为空条件操作符\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//4.37 空条件操作符\n",
    "#nullable enable  //enable\"、\"disable\" 或 \"restore\"\n",
    "string[]? segments=null;\n",
    "int? length=segments?.Length;\n",
    "//string? uri=null;\n",
    "if (length is object && length!=0)\n",
    "{\n",
    "    uri=string .Join('/',segments!);\n",
    "}\n",
    "    // 带数组访问器的空条件\n",
    "    // 假设我们知道至少有一种元素\n",
    "    //uri=segments?[0]\n",
    "    if (uri is null ||length is 0)\n",
    "    {\n",
    "        System.Console.WriteLine(\"没有可以合并的部分.\");\n",
    "    }else\n",
    "    {\n",
    "        System.Console.WriteLine( $\"Uri:{uri}\");\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "string[]? segments=null;\n",
    "int? length=(segments!=null)? (int?)segments.Length:null;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "segments?[0]?.ToLower().StartsWith(\"file:\");//tartsWith确定此字符串实例的开头是否与指定的字符串匹配。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//声明空字符串数组\n",
    "string?[]? segments;//数组及其元素均声明为可空。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [
       "c#",
       "C#"
      ],
      "languageName": "C#",
      "name": "csharp"
     },
     {
      "aliases": [],
      "languageName": "KQL",
      "name": "kql"
     },
     {
      "aliases": [
       "frontend"
      ],
      "languageName": null,
      "name": "vscode"
     },
     {
      "aliases": [
       "f#",
       "F#"
      ],
      "languageName": "F#",
      "name": "fsharp"
     },
     {
      "aliases": [],
      "languageName": null,
      "name": ".NET"
     },
     {
      "aliases": [],
      "languageName": "HTML",
      "name": "html"
     },
     {
      "aliases": [],
      "languageName": "Mermaid",
      "name": "mermaid"
     },
     {
      "aliases": [
       "powershell"
      ],
      "languageName": "PowerShell",
      "name": "pwsh"
     },
     {
      "aliases": [],
      "languageName": "SQL",
      "name": "sql"
     },
     {
      "aliases": [],
      "name": "value"
     },
     {
      "aliases": [
       "js"
      ],
      "languageName": "JavaScript",
      "name": "javascript"
     },
     {
      "aliases": [],
      "name": "webview"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
